package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.Utils.WxOrderStatusUtils;
import com.cskaoyan.bean.mallbean.Order;
import com.cskaoyan.bean.mallbean.OrderGoods;
import com.cskaoyan.service.mallservice.OrderService;
import com.cskaoyan.vo.BaseRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ked
 * @createTime 2021年05月12日 16:30:00
 */

@RestController
public class WxOrderController {

    @Autowired
    OrderService orderService;

    /**
     /**
     * 提交订单
     * 1. 创建订单和订单商品;
     * 2. 购物车
     * 3. 优惠券已用;
     * 4. 商品货品库存
     *
     */

    @PostMapping("wx/order/submit")
    public BaseRespVo orderSubmit(@RequestBody Map<String,Object> map){

        Integer userId = MyUtils.getUserId();

        Integer addressId = (Integer) map.get("addressId");
        Integer cartId = (Integer) map.get("cartId");
        Integer couponId = (Integer) map.get("couponId");
        String message = (String) map.get("message");
        Integer orderId = orderService.orderSubmit(addressId, cartId, couponId, message, userId);
        if(orderId == null){
            return BaseRespVo.fail742("商品已售完");
        }
        Map<String,Integer> data = new HashMap<>();
        data.put("orderId",orderId);
        return BaseRespVo.ok(data,"成功");
    }


    /**
     * 获得所有订单
     * @param showType 0:所有，1待付款101，2代发货201，3待收货301，4待评价401和402
     *
     */
    @GetMapping("wx/order/list")
    public BaseRespVo orderList(@NotNull Integer showType, @NotNull Integer page, @NotNull Integer size){
        Integer userId = MyUtils.getUserId();
        Map<String, Object> data = orderService.queryOrder(page, size, userId, showType);
        return BaseRespVo.ok(data,"成功");
    }

    @GetMapping("wx/order/detail")
    public BaseRespVo orderDetail(@NotNull Integer orderId){

        List<Order> orders = orderService.queryOrderDetail(orderId);
        List<OrderGoods> orderGoods = orderService.queryOrderGoods(orderId);
        WxOrderStatusUtils wxOrderStatusUtils = new WxOrderStatusUtils();

        Map<String,Object> data = new HashMap<>();
        Map<String,Object> orderInfo = new HashMap<>();
        Map<String,Object> expressInfo = new HashMap<>();

        orderInfo.put("actualPrice",orders.get(0).getActualPrice());
        orderInfo.put("addTime",orders.get(0).getAddTime());
        orderInfo.put("address",orders.get(0).getAddress());
        orderInfo.put("consignee",orders.get(0).getConsignee());
        orderInfo.put("couponPrice",orders.get(0).getCouponPrice());
        orderInfo.put("freightPrice",orders.get(0).getFreightPrice());
        orderInfo.put("goodsPrice",orders.get(0).getGoodsPrice());
        orderInfo.put("handleOption", wxOrderStatusUtils.handleOption(orders.get(0).getOrderStatus()));
        orderInfo.put("id",orders.get(0).getId());
        orderInfo.put("mobile",orders.get(0).getMobile());
        orderInfo.put("orderSn",orders.get(0).getOrderSn());
        orderInfo.put("orderStatusText",wxOrderStatusUtils.orderStatus(orders.get(0).getOrderStatus()));

        expressInfo.put("shipperName",orders.get(0).getShipChannel());
        expressInfo.put("logisticCode",orders.get(0).getShipSn());

        data.put("orderGoods",orderGoods);
        data.put("orderInfo",orderInfo);
        data.put("expressInfo",expressInfo);

        return BaseRespVo.ok(data,"成功");
    }

    /**
     *
     * 设置订单取消状态；
     * 商品库存；
     * 优惠券；
     */

    @PostMapping("wx/order/cancel")
    public BaseRespVo orderCancel(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        orderService.orderCancel(orderId, (short) 102);
        return BaseRespVo.ok();
    }

    @PostMapping("wx/order/refund")
    public BaseRespVo orderRefund(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        orderService.orderStatusUpdate(orderId, (short) 202);
        return BaseRespVo.ok();
    }

    @PostMapping("wx/order/delete")
    public BaseRespVo orderDelete(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        orderService.orderDelete(orderId);
        return BaseRespVo.ok();
    }

    @PostMapping("wx/order/confirm")
    public BaseRespVo orderConfirm(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        orderService.orderStatusUpdate(orderId, (short) 401);
        return BaseRespVo.ok();
    }

    @GetMapping("wx/order/goods")
    public BaseRespVo orderCommentWrite(@NotNull Integer orderId,@NotNull Integer goodsId){
        OrderGoods orderGood = orderService.queryOrderGoodsSingle(orderId,goodsId);
        return BaseRespVo.ok(orderGood,"成功");
    }

    /**
     * 提交评论→更新评论表，更新order，orderGoods，comment
     * order       `comments` smallint(6) DEFAULT '0' COMMENT '待评价订单商品数量',
     *             `update_time` datetime DEFAULT NULL COMMENT '更新时间',
     *
     * orderGoods  `comment` int(11) DEFAULT '0' COMMENT '订单商品评论，如果是-1，则超期不能评价；
     *                                                               如果是0，则可以评价；
     *                                                               如果其他值，则是comment表里面的评论ID。'
     *             `update_time` datetime DEFAULT NULL COMMENT '更新时间',
     *
     * comment     `id` int(11) NOT NULL AUTO_INCREMENT,
     *             `value_id` int(11) NOT NULL DEFAULT '0' COMMENT '如果type=0，则是商品评论；如果是type=1，则是专题评论。',
     *             `type` tinyint(3) NOT NULL DEFAULT '0' COMMENT '评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论；如果type=3，则是订单商品评论。',
     *             `content` varchar(1023) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '评论内容',
     *             `user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户表的用户ID',
     *             `has_picture` tinyint(1) DEFAULT '0' COMMENT '是否含有图片',
     *             `pic_urls` varchar(1023) DEFAULT NULL COMMENT '图片地址列表，采用JSON数组格式',
     *             `star` smallint(6) DEFAULT '1' COMMENT '评分， 1-5',
     *             `add_time` datetime DEFAULT NULL COMMENT '创建时间',
     *             `update_time` datetime DEFAULT NULL COMMENT '更新时间',
     *             `deleted` tinyint(1) DEFAULT '0' COMMENT '逻辑删除',
     *
     */

    @PostMapping("wx/order/comment")
    public BaseRespVo orderCommentCommit(@RequestBody Map<String,Object> map){

        Integer orderGoodsId = (Integer) map.get("orderGoodsId");
        Boolean hasPicture = (Boolean) map.get("hasPicture");
        String content = (String) map.get("content");
        List<String> picUrls = (List<String>) map.get("picUrls");
        Short star = Short.valueOf(String.valueOf(map.get("star")));


        Integer userId = MyUtils.getUserId();
        orderService.orderCommentUpdate(userId,content,hasPicture,orderGoodsId,picUrls,star);
        return BaseRespVo.ok();
    }

    @PostMapping("wx/order/prepay")
    public BaseRespVo orderPrepay(@RequestBody Map<String,Integer> orderId){

        return BaseRespVo.fail742("订单不能支付");
    }
}
